/*
 * Filename: e:\OwnProject\FlutterPro\flutter_app_01\lib\widgets\IAppbar.dart
 * Path: e:\OwnProject\FlutterPro\flutter_app_01\lib\widgets
 * Created Date: Tuesday, April 6th 2021, 11:53:07 am
 * Author: chenfayun
 * 
 * Copyright (c) 2021 Your Company
 */

import 'package:flutter/cupertino.dart';
import '../public.dart';

class IAppBar extends StatelessWidget implements PreferredSizeWidget {
  final String title;
  final bool backBtn;
  final Function? beforeBack;
  final Widget? right;
  final Widget? left;
  final bool bottomLine;
  final Color? backgroundColor;
  final double height;
  final TextStyle? titleStyle;
  const IAppBar(this.title,
      {Key? key,
      this.backBtn = false,
      this.right,
      this.height = 50.0,
      this.bottomLine = true,
      this.beforeBack,
      this.left,
      this.backgroundColor,
      this.titleStyle})
      : super(key: key);
  @override
  Size get preferredSize => Size.fromHeight(height);
  @override
  Widget build(BuildContext context) {
    Color _bgColor = backgroundColor ?? Colors.white;
    TextStyle _style = titleStyle ??
        TextStyle(
            fontSize: ScreenUtil.setSp(18).toDouble(),
            color: const Color(0xff333333));
    return Container(
      padding: EdgeInsets.only(top: ScreenUtil.topSafeHeight),
      height: height + ScreenUtil.topSafeHeight,
      decoration: BoxDecoration(
        color: _bgColor,
        border: bottomLine
            ? Border(
                bottom: BorderSide(
                    width: .5, color: Colors.grey[200] ?? Colors.grey),
              )
            : const Border(),
      ),
      child: Stack(
        children: <Widget>[
          backBtn
              ? Positioned(
                  left: 0,
                  top: 0,
                  bottom: 0,
                  child: GestureDetector(
                    onTap: () {
                      if (beforeBack != null) {
                        beforeBack!();
                      } else {
                        Navigator.of(context).pop();
                      }
                    },
                    child: left ??
                        Container(
                          color: _bgColor,
                          width: 44,
                          child: const Center(
                            child: Icon(CupertinoIcons.chevron_left,
                                color: Colors.blueAccent, size: 30),
                          ),
                        ),
                  ),
                )
              : const SizedBox(),
          Container(
            alignment: Alignment.center,
            margin: const EdgeInsets.only(left: 50, right: 50),
            child: Text(title,
                maxLines: 1, overflow: TextOverflow.ellipsis, style: _style),
          ),
          Positioned(
            right: 15,
            top: 0,
            bottom: 0,
            child: right ?? const SizedBox(),
          )
        ],
      ),
    );
  }
}
